#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Collections;
using Thn.Application.Sort;
using Thn.Collections;
#endregion

namespace Thn.Data
{
    /// <summary>
    /// Provide easy way to convert various type of results returned from a query execution
    /// </summary>
    public class ExecuteResult
    {
        #region Data
        private object mData;
        /// <summary>
        /// Gets/Sets the internal data
        /// </summary>
        public object Data
        {
            get { return mData; }
            set { mData = value; }
        }
        #endregion

        #region As List
        /// <summary>
        /// Gets the result as a list.
        /// <para>If there are no data, an empty ArrayList is returned</para>
        /// <para>If there are multiple object types, the very first one is returned</para>
        /// </summary>        
        public IList AsList
        {
            get 
            {
                IList result = null;

                if (mData == null) result = new Thn.Collections.ArrayList();
                else if (mData is IList) result = (IList)mData;
                else
                {
                    result = new Thn.Collections.ArrayList();
                    result.Add(mData);
                }
                //else throw new NotImplementedException("Sorry AsList can only interpret null or IList result at the mean time");

                return result;
            }
        }
        #endregion

        #region As Sorted List
        /// <summary>
        /// Gets the results as an ordered list.
        /// </summary>
        /// <param name="fields">Names of the fields to be sorted</param>
        public IList AsSortedList(params string[] fields)
        {
            return AsSortedList(SortDirection.Ascending, fields);
        }

        /// <summary>
        /// Gets the results as an ordered list.
        /// </summary>
        /// <param name="direction">Sort direction applicable to all fields</param>
        /// <param name="fields">Names of the fields to be sorted</param>
        public IList AsSortedList(SortDirection direction, params string[] fields)
        {
            IList result = AsList;

            if (result.Count > 0)
            {
                if (result is Thn.Collections.ArrayList)
                {
                    Thn.Collections.ArrayList lst = (Thn.Collections.ArrayList)result;
                    lst.Sort(direction, fields);
                }
                else
                {
                    Sorter sorter = new Sorter();
                    for (int i = 0; i < fields.Length; i++)
                    {
                        sorter.SetDirection(fields[i], direction);
                    }
                    sorter.Sort(result);
                }
            }

            return result;
        }
        #endregion

        #region As Single Object
        /// <summary>
        /// Gets the result as a single object.
        /// <para>If the result is a list, return the very first item</para>
        /// </summary>
        public object AsSingleObject
        {
            get
            {
                object result = null;

                if (mData != null)
                {
                    if (mData is IList)
                    {
                        IList lst = (IList)mData;
                        if (lst.Count > 0) result = lst[0];
                    }
                    else result = mData;
                }

                return result;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public ExecuteResult() 
        { }

        /// <summary>
        /// Full constructor
        /// </summary>
        /// <param name="data">internal data. Can be an IList or a single object</param>
        public ExecuteResult(object data)
        {
            mData = data;
        }
        #endregion
    }
}
